package com.yidiandao.business.data.model;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;

import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.request.AddConnectionRequest;
import com.yidiandao.business.data.net.BaseResponse;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.ui.widget.scanner.ZXingConstants;
import com.yidiandao.business.ui.widget.scanner.camera.CameraManager;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

/**
 * 我的  绑定二维码 邀请码
 * <p>
 * Created by SunDing(dindding@163.com) on 2017/3/24 10:19
 */
public class BindModel implements IBindModel {
    NetBiz mNetBiz = new NetBiz();
    /**
     * 是否打开闪关灯
     */
    boolean flag = true;
    @Override
    public void bindQrCode(String tag, String url, final OnAccessListener listener) {
        AddConnectionRequest request = new AddConnectionRequest();
        request.setTag(tag);
        request.setDirectUrl(url);
        request.setAppView("1");
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.sendGetRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (null != listener) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (null != listener) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (null != listener) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (null != listener) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void bindInviteCode(String tag, String code, final OnAccessListener listener) {
        AddConnectionRequest request = new AddConnectionRequest();
        request.setTag(tag);
        request.setUrl(Urls.BIND_SALES_CODE);
        request.setSalesCode(code);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.sendGetRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (null != listener) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (null != listener) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (null != listener) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (null != listener) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void controlLight() {
        if (flag) {
            flag = false;
            // 开闪光灯
            CameraManager.get().openLight();
        } else {
            flag = true;
            // 关闪光灯
            CameraManager.get().offLight();
        }
    }

    @Override
    public void choosePic(Activity activity) {
        Intent innerIntent = new Intent();
        if (Build.VERSION.SDK_INT < 19) {
            innerIntent.setAction(Intent.ACTION_GET_CONTENT);
        } else {
            innerIntent.setAction(Intent.ACTION_OPEN_DOCUMENT);
        }
        innerIntent.setType("image/*");
        Intent wrapperIntent = Intent.createChooser(innerIntent, "选择二维码图片");
        activity.startActivityForResult(wrapperIntent, ZXingConstants.ScanPhotosRequestCode);
    }
}
